<%

import mephisto.*
import mephisto.services.impl.*

argpos = request.getPathInfo().indexOf(doc.name) + doc.name.length() + 1

if(argpos > 0 &&  request.getPathInfo().length() > argpos) {
   arg = request.getPathInfo().substring(argpos)
   distribution = doc.name
   app = null
   
   if (arg.indexOf("/packages/")>0) {
     tmp = arg.split("/")
     pkg = tmp[3]
     println("1 $pkg")
     context.put("pkg", pkg)
     result = xwiki.getDocument("mephisto.package").getRenderedContent()
     println(result)

   } else {

    tmp = arg.split("/",2)
    app = tmp[1]
    println("1 $app")
    pkgList = new File("/home/slauriere/distributions/cooker/packages.list").text
    lines = pkgList.split("\n")
    for (line in lines) {
      pkgAttr = line.split("\t")
      if (pkgAttr[1].equals(app)) {
       link = xwiki.getURL(doc.fullName)+"/${arg}/packages/"+pkgAttr[0]
       println("* <a href='$link'>"+pkgAttr[0]+"</a>")
      }
    }
   

   //display wiki info if any, or link for creating wiki
   if (xwiki.exists("software.${app}")) { 
       result = xwiki.getDocument("software.${app}").getRenderedContent()
       println(result)
   } else {
      println("Create a wiki for ${app}: click <a href='"+xwiki.getURL("software.${app}", "edit")+"'>here</a>.")
   }


   if (app != null) {

   //display existing bugs
   println("1.1 Bugs")

  mephisto = Mephisto.getInstance()
  rdf = mephisto.getRdfService()
  utils = new Utils()

   println("<table class='table-1'>")
   sparql= "select * where { ?bug bom:isIssueOf <http://mephistopedia.net/software/$app> . ?bug rdfs:label ?label}"

   array = rdf.runQuery(sparql)
   println("<tr><td>Software</td></tr>")
   for (i = 0; i < array.length(); i++) {
         entry = array.get(i)
         value = entry.bug.value
         label = value
         if (entry.label)
           label = entry.label.value
         if (value.length()>0) {
            link = xwiki.getURL("mephisto.software")+"/"+value
            println("<tr><td><a href='$link'>$label</a></td></tr>")
         }
         
   }
   println("</table>")


   }
   


}



} else {

println("1 Mandriva Cooker software")
  offset = 0
  limit = 1000

  mephisto = Mephisto.getInstance()
  rdf = mephisto.getRdfService()
  rdf.registerNamespace("mephisto", "http://mephistopedia.net/2009/03/14/mephisto#")
  utils = new Utils()

println("<table class='table-1'>")
try {
   sparql= "select * where {?software a mephisto:Software } order by ?software offset $offset limit $limit"

   array = rdf.runQuery(sparql)
   ns = "http://mephistopedia.net/software/"
   index = ns.length()
   //out.println("{{class=table-1}}\n!! Software name")
   println("<tr><td>Software</td></tr>")
   for (i = 0; i < array.length(); i++) {
         entry = array.get(i)
         value = entry.software.value
         value = value.substring(index).trim()
         if (value.length()>0) {
            link = xwiki.getURL("mephisto.software")+"/"+value
            println("<tr><td><a href='$link'>$value</a></td></tr>")
         }
         
   }


offset = offset+limit

} catch (Exception e) {

println()
println(e)
}
println("</table>")

}
%>

